//axios 配置
define(function(require, exports, module) {
    //引用axios
    const axios=require('axios');
    const base64=require('Base64');
    const router=require('router');
    // const store=require('store');
    const weui=require('weui');
    const sdk=require("sdk");
    // 创建axios实例
    const service = axios.create({
        //baseURL: '/myapi', // api的base_url
        baseURL:'/mobile/app/core',
        headers:{
            'Content-Type':'application/json;utf-8'
        },
        timeout: 30000 // 请求超时时间
    });
    // request拦截器
    service.interceptors.request.use(function(config){
        // post 数据base64加密
        if(typeof config.data==="object") {
            config.data = base64.encode(JSON.stringify(config.data));
        }
        return config;
    }, function(error) {
        return Promise.reject(error);
    });

    // respone拦截器
    service.interceptors.response.use(function (response) {
        var res=response.data;
        if(response.status===200){
            if(typeof response.data==='string'){
                try {
                    res=JSON.parse(base64.decode(response.data));
                }catch(e){
                    res=JSON.parse(response.data);
                }
            }else if(typeof response.data==='object'){
                res=response.data;
            }
            if(res.user_token_invalid){
                window.localStorage.removeItem('USERBIKE_USER_INFO');
                weui.topTips("登录已失效！",{callback:function () {
                    sdk.closeWindow();
                    router.push({path:'/phone'});
                }});
            }
            //console.log(res);
            return res;
        }else {
            return Promise.reject(res);
        }
    }, function(error){
        return Promise.reject(error);
    });
    module.exports = service;
});